package com.xyht.medicine_management_system.modules.kuaidi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.kuaidi.entity.MmsKuaidiSetting;
import com.xyht.medicine_management_system.modules.kuaidi.entity.req.MmsKuaidiSettingReq;
import com.xyht.medicine_management_system.modules.kuaidi.entity.resp.MmsKuaidiSettingResp;
import com.xyht.medicine_management_system.modules.kuaidi.mapper.MmsKuaidiSettingMapper;
import com.xyht.medicine_management_system.modules.kuaidi.service.MmsKuaidiSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.kuaidi.constant.IsOpenFlagConstant.IS_OPEN_FLAG_NO;
import static com.xyht.medicine_management_system.modules.kuaidi.constant.IsOpenFlagConstant.IS_OPEN_FLAG_YES;

/**
 * <p>
 * 发货地址配置 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-07-02
 */
@Service
public class MmsKuaidiSettingServiceImpl extends ServiceImpl<MmsKuaidiSettingMapper, MmsKuaidiSetting> implements MmsKuaidiSettingService {
    @Resource
    private MmsKuaidiSettingMapper mmsKuaidiSettingMapper;

    @Override
    @Transactional
    public ResponseResult addSetting(MmsKuaidiSettingReq settingReq) {
        if (isNullOrEmpty(settingReq.getSendName(), settingReq.getSendTel(),
                settingReq.getSendAddr(), settingReq.getIsOpen())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (settingReq.getIsOpen() != IS_OPEN_FLAG_YES && settingReq.getIsOpen() != IS_OPEN_FLAG_NO) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //有且只有一个地址开启
        if (settingReq.getIsOpen() == IS_OPEN_FLAG_YES) {
            MmsKuaidiSetting updateSetting = new MmsKuaidiSetting();
            updateSetting.setIsOpen(IS_OPEN_FLAG_NO);
            mmsKuaidiSettingMapper.update(updateSetting, new LambdaQueryWrapper<MmsKuaidiSetting>()
                    .eq(MmsKuaidiSetting::getIsOpen, IS_OPEN_FLAG_YES));
        }

        MmsKuaidiSetting setting = new MmsKuaidiSetting();
        BeanUtils.copyProperties(settingReq, setting);
        this.save(setting);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteSetting(MmsKuaidiSettingReq settingReq) {
        if (isNullOrEmpty(settingReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        mmsKuaidiSettingMapper.deleteById(settingReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSetting(MmsKuaidiSettingReq settingReq) {
        if (isNullOrEmpty(settingReq.getId(), settingReq.getSendName(), settingReq.getSendTel(),
                settingReq.getSendAddr())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsKuaidiSetting updateSetting = new MmsKuaidiSetting();
        BeanUtils.copyProperties(settingReq, updateSetting);
        updateSetting.setIsOpen(null);
        mmsKuaidiSettingMapper.updateById(updateSetting);
        return CommonResult.success();
    }

    @Override
    public ResponseResult switchSetting(MmsKuaidiSettingReq settingReq) {
        if (isNullOrEmpty(settingReq.getId(), settingReq.getIsOpen())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        if (settingReq.getIsOpen() != IS_OPEN_FLAG_YES && settingReq.getIsOpen() != IS_OPEN_FLAG_NO) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        //有且只有一个地址开启
        if (settingReq.getIsOpen() == IS_OPEN_FLAG_YES) {
            MmsKuaidiSetting updateSetting = new MmsKuaidiSetting();
            updateSetting.setIsOpen(IS_OPEN_FLAG_NO);
            mmsKuaidiSettingMapper.update(updateSetting, new LambdaQueryWrapper<MmsKuaidiSetting>()
                    .eq(MmsKuaidiSetting::getIsOpen, IS_OPEN_FLAG_YES));
        }

        MmsKuaidiSetting updateSetting = new MmsKuaidiSetting();
        updateSetting.setId(settingReq.getId());
        updateSetting.setIsOpen(settingReq.getIsOpen());
        mmsKuaidiSettingMapper.updateById(updateSetting);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getSettingList(String searchStr, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<MmsKuaidiSetting> queryWrapper = new LambdaQueryWrapper<>();
        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.and(wrapper -> {
                wrapper.like(MmsKuaidiSetting::getSendName, searchStr)
                        .or()
                        .like(MmsKuaidiSetting::getSendTel, searchStr)
                        .or()
                        .like(MmsKuaidiSetting::getSendAddr, searchStr);
            });
        }
        queryWrapper.orderByDesc(MmsKuaidiSetting::getIsOpen)
                .orderByDesc(MmsKuaidiSetting::getCreateTime);

        Page<MmsKuaidiSetting> page = new Page<>(pageNum, pageSize);
        mmsKuaidiSettingMapper.selectPage(page, queryWrapper);

        List<MmsKuaidiSettingResp> list = page.getRecords()
                .stream()
                .map(setting -> {
                    MmsKuaidiSettingResp settingResp = new MmsKuaidiSettingResp();
                    BeanUtils.copyProperties(setting, settingResp);

                    return settingResp;
                }).collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getSettingDetail(String settingId) {
        MmsKuaidiSettingResp resp = new MmsKuaidiSettingResp();
        MmsKuaidiSetting setting = mmsKuaidiSettingMapper.selectById(settingId);
        if (setting == null){
            return CommonResult.success(resp);
        }
        BeanUtils.copyProperties(setting, resp);
        return CommonResult.success(resp);
    }

}
